package com.cadre.web.controller.officer;

import com.cadre.common.utils.R;
import com.cadre.common.utils.oldutil.DateUtil;
import com.cadre.system.service.ExcelImpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

@Api(value = "Excel导入", tags = {"excel导入通用类"})
@RequestMapping({"/person/excel"})
@RestController
/* loaded from: ExcelImpController.class */
public class ExcelImpController {
    @Autowired
    private ExcelImpService excelImpService;
    private String ImpTable;
    private String TempTable;
    private Map<String, String> ExcelAndTempTable = new HashMap();
    private Map<String, String> ImpTableAndTempTable = new HashMap();

    private String GetImpTable(String tableName) {
        String str = "";
        if (tableName.equals("A91")) {
            str = "A91";
        }
        return str;
    }

    private String GetTempTable(String tableName) {
        String str = "";
        if (tableName.equals("A91")) {
            str = "A91Temp";
        }
        return str;
    }

    private Map<String, String> GetExcelAndTempTable(String tableName) {
        Map<String, String> map = new HashMap<>();
        if (tableName.equals("A91")) {
            map.put("1", "A0106");
            map.put("2", "A9102");
            map.put("3", "A9101");
        }
        return map;
    }

    private Map<String, String> GetImpTableAndTempTable(String tableName) {
        Map<String, String> map = new HashMap<>();
        if (tableName.equals("A91")) {
            map.put("A9102", "A9102");
            map.put("A9101", "A9101");
            map.put("personid", "personid");
            map.put("flowid", "flowid");
        }
        return map;
    }

    private String getExcelname(String flag) {
        String str = "";
        if (flag.equals("A91")) {
            str = "干部实绩";
        }
        if (flag.equals("pxrypxxx")) {
            str = "干部脱产培训情况统计表";
        }
        return str;
    }

    @PostMapping({"/downloadFile"})
    @ApiOperation("下载模板")
    public void downloadFile(@RequestParam String tableName, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String str = getExcelname(tableName);
        getExcel(new ClassPathResource("/excel\\" + str + ".xls"), str + DateUtil.dateToString(new Date(), "yyyyMMddHHmmss") + ".xls", response);
    }

    public static void getExcel(ClassPathResource cpr, String fileName, HttpServletResponse response) {
        try {
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO-8859-1"));
            InputStream in = cpr.getInputStream();
            OutputStream out = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[2048];
            while (true) {
                int b = in.read(buffer);
                if (b != -1) {
                    out.write(buffer, 0, b);
                } else {
                    in.close();
                    out.flush();
                    out.close();
                    return;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping({"/impExcel"})
    @ApiOperation("导入---excel")
    @ResponseBody
    public R impExcel(@RequestParam String tableName, @RequestParam("file") MultipartFile file, HttpServletResponse response) {
        this.TempTable = GetTempTable(tableName);
        this.ImpTable = GetImpTable(tableName);
        this.ExcelAndTempTable = GetExcelAndTempTable(tableName);
        return R.ok(this.excelImpService.readExcelInTempTable(file, this.ImpTable, this.TempTable, this.ExcelAndTempTable, tableName));
    }

    @GetMapping({"/list"})
    @ApiOperation("根据状态获取list")
    public R list(@RequestParam String tableName, @RequestParam String status) {
        this.TempTable = GetTempTable(tableName);
        return R.ok().put("data", (Object) this.excelImpService.getListForStatus(this.TempTable, status));
    }

    @PostMapping({"/delete"})
    @ApiOperation("正常数据-删除")
    public R delete(@RequestParam String tableName, @RequestBody String thisIds) {
        this.TempTable = GetTempTable(tableName);
        this.excelImpService.deleteByIds(this.TempTable, Arrays.asList(thisIds.split(",")));
        return R.ok();
    }

    @PostMapping({"/inputTable"})
    @ApiOperation("正常数据-导入数据库")
    public R inputTable(@RequestParam String tableName, @RequestParam String status) {
        this.ImpTable = GetImpTable(tableName);
        this.TempTable = GetTempTable(tableName);
        this.ImpTableAndTempTable = GetImpTableAndTempTable(tableName);
        this.excelImpService.insertForTempTable(this.ImpTable, this.TempTable, status, this.ImpTableAndTempTable);
        return R.ok("操作成功!");
    }

    @PostMapping({"/updateTable"})
    @ApiOperation("冲突数据-更新到数据库")
    public R updateTable(@RequestParam String tableName, @RequestBody String thisIds) {
        this.ImpTable = GetImpTable(tableName);
        this.TempTable = GetTempTable(tableName);
        this.ImpTableAndTempTable = GetImpTableAndTempTable(tableName);
        this.excelImpService.updateImpTable(this.ImpTable, this.TempTable, thisIds, tableName);
        return R.ok("操作成功!");
    }

    @PostMapping({"/exportExcel"})
    @ApiOperation("未匹配数据-导出excel")
    public void exportExcel(@RequestParam String tableName, @RequestParam String status, HttpServletResponse response) {
        this.TempTable = GetTempTable(tableName);
        String str = getExcelname(tableName);
        exportA91(new ClassPathResource("/excel\\" + str + ".xls"), this.TempTable, response, str + DateUtil.dateToString(new Date(), "yyyyMMddHHmmss") + ".xls", status);
    }

    private void exportA91(ClassPathResource cpr, String tempTable, HttpServletResponse response, String fileOutName, String status) {
        try {
            HSSFWorkbook wb = new HSSFWorkbook(cpr.getInputStream());
            List<Map<String, Object>> list = this.excelImpService.getListForStatus(tempTable, status);
            HSSFSheet sheet = wb.getSheetAt(0);
            for (int i = 0; i < list.size(); i++) {
                HSSFRow row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(isNull(list.get(i).get("NAME")));
                row.createCell(1).setCellValue(isNull(list.get(i).get("A0106")));
                row.createCell(2).setCellValue(isNull(list.get(i).get("A9102")));
                row.createCell(3).setCellValue(isNull(list.get(i).get("A9101")));
            }
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileOutName.getBytes("gb2312"), "ISO-8859-1") + ".xls");
            InputStream in = cpr.getInputStream();
            OutputStream out = new BufferedOutputStream(response.getOutputStream());
            wb.write(out);
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String isNull(Object obj) {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }
}
